package com.chen.project.white_jotter.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private SiteProps siteProps;

    // 虚拟路径映射
    // web路径映射为文件路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(siteProps.getOsType().contains("Windows")){
            registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + siteProps.getFilePath());
        }else if(siteProps.getOsType().contains("Linux")) {
            System.out.println("linux 文件-url映射");
            registry.addResourceHandler("/api/file/**").addResourceLocations("file://" + siteProps.getFilePath());
        }
    }

    // 跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST","PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*");
    }
}
